// pages/user/profile/cert/card/index.js
import constant from '../../../../../utils/constant'
import { config } from '../../../../../config'
import api from '../../../../../api/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: wx.getStorageSync(constant.USER_INFO),
    ocr:{
      birth: '',
      id: '',
      name: '',
      sex: '',
      addr: '',
      nationality: '',
    },
    identity: '',
    username: '',
    sex: '',
    nationality: '',
    authBirth: '',
    authAddr: '',
    file: '',
    img: '',
    areas: [],
    sub: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 选择上传方式
   * @param {*} e 
   */
  chooseImg(e) {
    if (this.NextTap) {
      return;
    }
    this.NextTap = true;
    setTimeout(() => {
      this.NextTap = false;
      }, 2000)//1.5秒之后可以再次点击，防止用户重复点击
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: (res) => {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            this.chooseWxImage('album')//相册
          } else if (res.tapIndex == 1) {
            this.chooseWxImage('camera')//拍照
          }
        }
      }
    })
  },
    /**
   * 上传照片
   * @param {*} type 
   */
  chooseWxImage: function (type) {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: [type],
      sizeType: ['compressed'],
      camera: 'front',
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath
        console.log(tempFilePath)
        wx.compressImage({
          src: tempFilePath,
          compressedHeight: 320,
          success: function (rs) {
            console.log(rs)
            api.cert.ocrIdCard(rs.tempFilePath).then(r=>{
              console.log(r)
              that.setData({
                file: rs.tempFilePath,
                ocr:{
                  birth: r.birthday,
                  id: r.id,
                  name: r.name,
                  sex: r.sex,
                  addr: r.addr,
                  nationality: r.nationality
                },
                identity: r.id,
                username: r.name,
                sex: r.sex,
                nationality: r.nationality,
                authBirth: r.birthday,
                authAddr: r.addr,
                sub: 1
              })
            })
          }
        })
      }

    })
  },
  onPicker(e){
    let inx = e.currentTarget.dataset.index
    let v = e.detail
    switch(inx){
      case 'name':
        let name = v.value
        this.setData({
          username: name
        })
        break
      case 'cardId':
        let id = v.value
        this.setData({
          identity: id
        })
        break
      case 'areas':
        console.log(v)
        let areas = []
        areas.push(v.name[0])
        areas.push(v.name[1])
        this.setData({
          areas: areas
        })
       break 
    }
  },

  onOK(){
    if(this.data.sub == 0){
      wx.$msg('请先上传身份证')
    }else{
      console.log(this.data)
      console.log(this.data.identity.trim().length)
      if(this.data.areas.length == 0 || this.data.areas[1] == '请选择'){
        console.log(1)
        wx.$msg('请确认选择家乡在哪')
      }else if(wx.$isNull(this.data.username)){
        wx.$msg('请确认姓名')
      }else if(wx.$isNull(this.data.identity)||this.data.identity.trim().length!=18){
        wx.$msg('请确认身份证号码')
      }else{
        let ocrName = this.data.ocr.name.trim()
        let name = this.data.username.trim()
        let ocrId = this.data.ocr.id.trim()
        let id = this.data.identity.trim()
        if(ocrId.length == 18){
          let num = 0
          for(let i = 0; i<ocrId.length; i++){
            if(ocrId.charAt(i)!=id.charAt(i)){
              num = num + 1
            }
          }
          if(num>2){
            wx.$msg('认证失败')
            return
          }
          
        }
        let n = 0
        for(let i = 0; i<name.length; i ++){
          if(name.charAt(i) != ocrName.charAt(i)){
            n = n + 1
          }
          if(n>1){
            wx.$msg('认证失败')
            return
          }
        }
        
      }
    }
  }



})